package com.zcc.config;

import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class MyLocalResolve implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取语言参数，界面发送的请求所携带的属性
        String language = request.getParameter("l");
        //设置默认值，默认的本地化
        Locale locale = Locale.getDefault();

        //如果语言设置不为空
        if (language != null&&!language.isEmpty()) {
            //将字符串分割，因为候选的语言请求是en_US或者zh_CN
            String[] s = language.split("_");
            //本地化设置，取分割后的字符串的前两个，也就是en/zh
            locale = new Locale(s[0],s[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
